package io.renren.modules.jsf.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.renren.modules.jsf.entity.JsfTotalMoneyEntity;
import io.renren.modules.jsf.entity.YuyueEntity;
import io.renren.modules.jsf.service.JsfTotalMoneyService;
import io.renren.modules.jsf.service.YuyueService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.jsf.entity.JsfYejiEntity;
import io.renren.modules.jsf.service.JsfYejiService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.common.utils.ShiroUtils.getUserId;


/**
 * @author chen
 * @email sun@gmail.com
 * @date 2022-04-10 00:35:10
 */
@RestController
@RequestMapping("jsf/jsfyeji")
public class JsfYejiController {
    @Autowired
    private JsfYejiService jsfYejiService;

    @Autowired
    SysUserService userService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("jsf:jsfyeji:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = jsfYejiService.queryPage(params);
        if (getUserId() != 1) {
            SysUserEntity userEntity = userService.getById(getUserId());
            List<JsfYejiEntity> list = (List<JsfYejiEntity>) page.getList();
            List<JsfYejiEntity> data = list.stream().filter(t -> {
                return t.getTeacherName().equals(userEntity.getUsername());
            }).collect(Collectors.toList());
            page.setList(data);
        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("jsf:jsfyeji:info")
    public R info(@PathVariable("id") Integer id) {
        JsfYejiEntity jsfYeji = jsfYejiService.getById(id);

        return R.ok().put("jsfYeji", jsfYeji);
    }

    @Autowired
    JsfTotalMoneyService totalMoneyService;

    @Autowired
    YuyueService yuyueService;

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("jsf:jsfyeji:save")
    public R save(@RequestBody JsfYejiEntity jsfYeji) {
        SysUserEntity userEntity = userService.selectOneByName(jsfYeji.getUsername());
        jsfYeji.setUserId(Math.toIntExact(userEntity.getUserId()));
        //入库预约
        YuyueEntity yuyueEntity = new YuyueEntity();
        yuyueEntity.setUsername(jsfYeji.getUsername());
        yuyueEntity.setTeachername(jsfYeji.getTeacherName());
        yuyueEntity.setCoursename(jsfYeji.getCoursename());
        yuyueEntity.setMoney(Double.valueOf(jsfYeji.getMoney()));
        yuyueEntity.setCreatetime(new Date());
        yuyueService.save(yuyueEntity);

        //入库收支
        JsfTotalMoneyEntity totalMoneyEntity = new JsfTotalMoneyEntity();
        totalMoneyEntity.setUserId(jsfYeji.getUserId());
        totalMoneyEntity.setUsername(jsfYeji.getUsername());
        totalMoneyEntity.setShouRu(jsfYeji.getMoney());
        totalMoneyEntity.setCreateTime(new Date());
        totalMoneyEntity.setMemo(jsfYeji.getCoursename() + "项目收入");
        totalMoneyService.save(totalMoneyEntity);

        //入库业绩
        jsfYeji.setCreateTime(new Date());
        jsfYejiService.save(jsfYeji);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("jsf:jsfyeji:update")
    public R update(@RequestBody JsfYejiEntity jsfYeji) {
        jsfYejiService.updateById(jsfYeji);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("jsf:jsfyeji:delete")
    public R delete(@RequestBody Integer[] ids) {
        jsfYejiService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
